package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.SpdbConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.spdb.ebank.SpdbMessage;

/**
 * @SpdbB2cServerRes.java
 * @author wanggang
 * @2013-12-10 下午5:08:12 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class SpdbB2cServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(SpdbB2cServerRes.class);

	@RequestMapping(value = "/spdbB2cServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- SpdbB2cServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		SpdbMessage spdbMessage = new SpdbMessage();
		spdbMessage.setBank(BankConstants.BANK_LOGO_SPDB);
		spdbMessage.setPlainMessage(request.getParameter("Plain"));
		spdbMessage.setSignMessage(request.getParameter("Signature"));
		if(spdbMessage.getPlainMessage()!=null){
			LinkedHashMap map = this.str2map(request.getParameter("Plain"));
			spdbMessage.setBankOrderId(map.get("TermSsn").toString());// 订单号
			spdbMessage.setAmount(new BigDecimal(map.get("TranAmt").toString()));// 交易金额
			spdbMessage.setRespCode(map.get("RespCode").toString());// 响应码（00：表示交易成功，A0表示B2C订单接收成功）
		}
        //银行业务产品00个人网银支付
		spdbMessage.setBankBusProd(BisBankBusinessProd._00);
		return spdbMessage;

	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		SpdbMessage spdbMessage =(SpdbMessage) result.getModel("message");
		if (spdbMessage != null) {
			if (SpdbConstants.RESPCODE_SUCCESS.equals(spdbMessage.getRespCode())) {
				orderInfo.setOrderId(spdbMessage.getOrderId());
				orderInfo.setAmt(spdbMessage.getAmount().toString());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("");
			} else {
				orderInfo.setOrderId(spdbMessage.getOrderId());
				orderInfo.setAmt(spdbMessage.getAmount().toString());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg("");
			}
		}
		return orderInfo;
	}

	/**
	 * 解析明文放入Map.
	 * 
	 * @param dataStr
	 * @return
	 */
	private LinkedHashMap str2map(String dataStr) {
		if (dataStr == null || dataStr.length() == 0) {
			return null;
		}
		LinkedHashMap retMap = new LinkedHashMap();
		String terms[] = dataStr.split("\\|");
		int len = terms.length;
		for (int i = 0; i < len; i++) {
			String term = terms[i];
			int index = term.indexOf("=");
			retMap.put(term.substring(0, index), term.substring(index + 1));
		}
		return retMap;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("Plain"))|| StringUtils.isEmpty(request.getParameter("Signature"))) {
		return false;
		}
		return true;
	}

}
